<%@ page language="java" contentType="text/html; charset=windows-1256" pageEncoding="windows-1256"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="common.Configuration" %>    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Liste Des Questionnaires</title>
<script language ="javascript">
function addlist(n){
	var lst = document.form1.list.value;
	if(n.checked) {
		lst = lst + "," + n.name;		
		}
	else {
		lst = lst.replace("," + n.name , "");		
	}
	document.form1.list.value = lst;
}

function Delete(){
	lst = document.form1.list.value;
	lst = lst.substr(1);
	document.form1.list.value = lst;
	if (lst == "") { alert ("No Questionnaires are selected !!!");}
}
function Edit () {
	var list = document.form1.list.value;
	var arr = list.split(",");
	if (arr.length != 2) {
		alert("Please Check Only One Questionnaire to Edit !!!");
		return false;
	}
	window.location.href="GoToEdit.jsp?qstid=" + arr[1];
}
function backhome () {
	
	window.location.href="Accueil.jsp";
}
</script>
</head>
<body>
<form name="form1" method="post">
<table width="100%">
<tr>
   <td align ="center" colspan="2"><b><u> Liste de Questionnaires</u></b></td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
 <td width ="50%">
	<table border ="1" >
	<tr>
		<td><b>  &nbsp;</b></td>
		<td><b>  Question ID   </b></td>
		<td><b>  Question Description </b></td>
	</tr>
<%
Statement stmt   = null;
Statement stmt2  = null;
ResultSet rst    = null;
ResultSet rstdlt = null;
stmt  = Configuration.GetStatement();
stmt2 = Configuration.GetStatement();
String deletlist = (String)request.getParameter("list");
     
     //Delete Questionnaires
     if (deletlist != null) { 
     String[] arr = deletlist.split(",");
 
	 for (int i = 0; i < arr.length ; i++) {
		 rstdlt = stmt.executeQuery("select * from question where idquestiongroup = '" + arr[i] + "'");
	     while(rstdlt.next())     
	      {int query1 = stmt2.executeUpdate(" DELETE FROM subquestion where idquestion = '" + rstdlt.getInt( "idquestion" ) + "'");} 
	     rstdlt.close();
		 int query3 = stmt2.executeUpdate(" DELETE FROM question where idquestiongroup = '" + arr[i] + "'");
		 int query4 = stmt2.executeUpdate(" DELETE FROM questiongroup where idquestiongroup = '" + arr[i] + "'");
	 }
	}
    
     // Load All Questionnaires
     int qstcount = 0;
	 try  { 
		  rst = stmt.executeQuery("select * from questiongroup");
          while(rst.next())     
           {              
       	    qstcount = qstcount + 1;
            String title = rst.getString("QGTitle"); 
            Integer id = rst.getInt( "idquestiongroup" ); 
            out.println("<tr><td><input type='checkbox' onClick='addlist(this);' name ='" + id.toString() + "' /></td><td>" + id.toString() + "</td><td>" + title + "</td></tr>" );                
           }          
          rst.close();         
	 }
	 catch( Exception e ) { 
	      System.out.println( "Exception Error:  " + e );       
     }
%>
</table>
</td>
 <td align="right" width="50%" valign="top">
	 <input type="submit" value=" Delete " onclick="Delete();"> 
	 <input  type="button" value="   Edit   " onclick="Edit();" >
	 <input  type="button" value =" <<-- Home " onclick="backhome()">
	 <input type="hidden" name="list" value=""> 
 </td>
</tr>
</table>
</form>
</body>
</html>